<template>
  <el-card class="page-container">
    <template #header>
      <div class="header">
        <span>重置密码</span>
      </div>
    </template>
    <el-form>
      <el-form-item label="旧密码">
        <el-input v-model="old_pwd" type="password" placeholder="请输入旧密码" />
      </el-form-item>
      <el-form-item label="新密码">
        <el-input v-model="new_pwd" type="password" placeholder="请输入新密码" />
      </el-form-item>
      <el-form-item label="确认密码">
        <el-input v-model="re_pwd" type="password" placeholder="请再次输入新密码" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="resetPassword">重置密码</el-button>
      </el-form-item>
    </el-form>
  </el-card>
</template>

<script setup>
import { ref } from 'vue';
import { ElMessage } from "element-plus";
import {updatePassword} from "@/api/admin.js";

const old_pwd = ref("");
const new_pwd = ref("");
const re_pwd = ref("");

const resetPassword = async () => {
  if (new_pwd.value !== re_pwd.value) {
    ElMessage.error("两次输入的密码不一致！");
    return;
  }
  try {
    const response = await updatePassword({
      old_pwd: old_pwd.value,
      new_pwd: new_pwd.value,
      re_pwd: re_pwd.value });
    if (response.code === 0) {
      ElMessage.success("密码重置成功！");
    }else {
      ElMessage.error("密码重置失败！");
    }

  } catch (error) {
    console.error('密码重置时出错：', error);
    ElMessage.error('密码重置时出错，请重试！');
  }
};
</script>

<style lang="scss" scoped>
.page-container {
  min-height: 100%;
  box-sizing: border-box;

  .header {
    display: flex;
    align-items: center;
    justify-content: space-between;
  }
}
</style>